package com.mvvmhabit.org.binding.viewadapter.smartrefresh

import androidx.databinding.BindingAdapter
import com.mvvmhabit.org.binding.command.BindingCommand
import com.scwang.smart.refresh.layout.SmartRefreshLayout

@BindingAdapter(value = ["onSmartRefreshCommand"])
fun onRefreshCommand(
    smartRefreshLayout: SmartRefreshLayout, onRefreshCommand: BindingCommand<Any>
) {
    smartRefreshLayout.setOnRefreshListener {
        it.finishRefresh(1000)
        onRefreshCommand.execute()
    }
}

@BindingAdapter(value = ["onSmartLoadMoreCommand"])
fun onLoadMoreCommand(
    smartRefreshLayout: SmartRefreshLayout, onLoadMoreCommand: BindingCommand<Any>
) {
    smartRefreshLayout.setOnLoadMoreListener {
        it.finishLoadMore(1000)
        onLoadMoreCommand.execute()
    }
}

@BindingAdapter(value = ["isEnableLoadMore"])
fun isEnableLoadMore(smartRefreshLayout: SmartRefreshLayout, isEnableLoadMore: Boolean) {
    smartRefreshLayout.setEnableLoadMore(isEnableLoadMore)
}

@BindingAdapter(value = ["isEnableRefresh"])
fun isEnableRefresh(smartRefreshLayout: SmartRefreshLayout, isEnableRefresh: Boolean) {
    smartRefreshLayout.setEnableRefresh(isEnableRefresh)
}